package org.cshoo.sys.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.ResultEvent;
	
	import org.cshoo.common.util.MessageUtil;
	import org.cshoo.sys.business.OrganizationDelegate;
	import org.cshoo.sys.event.DeleteOrganizationEvent;
	import org.cshoo.sys.model.SysModelLocator;
	import org.cshoo.sys.model.vo.Organization;
	
	public class DeleteOrganizationCommand implements ICommand, IResponder
	{
		private　var　_model:SysModelLocator　=　SysModelLocator.getInstance();
		
		private var organization:Organization;
		
		public function execute(event:CairngormEvent):void
		{
			organization = (event as DeleteOrganizationEvent).organization;
			var organizationDelegate:OrganizationDelegate = new OrganizationDelegate(this);
			organizationDelegate.deleteOrganization(organization);
		}
		
		public function result(event:Object):void
		{
			if((event as ResultEvent).result as Boolean)
			{
				if(organization.parent != null)
				{
					organization.parent.children.removeItemAt(organization.parent.children.getItemIndex(organization));
				}
				else
					_model.rootOrganizationCollection.removeItemAt(_model.rootOrganizationCollection.getItemIndex(organization));
				MessageUtil.showInfoMessage("组织机构数据删除成功");
			}
			else
				MessageUtil.showErrorMessage("组织机构数据删除失败");
		}
		
		public function fault(info:Object):void
		{
			Alert.show("服务器端已经停止");
		}
	}
}